package com.ruoyi.combat.service.apprisk.impl;

import java.util.ArrayList;
import java.util.List;

import com.ruoyi.combat.domain.apprisk.AppUserRegisterBody;
import com.ruoyi.combat.domain.apprisk.CombatAppRisk;
import com.ruoyi.combat.domain.apprisk.CombatPhoneApp;
import com.ruoyi.combat.mapper.apprisk.CombatAppRiskMapper;
import com.ruoyi.combat.service.apprisk.ICombatApplicationService;
import com.ruoyi.combat.service.apprisk.ICombatPhoneAppService;
import com.ruoyi.common.constant.Constants;
import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.MessageUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.framework.manager.AsyncManager;
import com.ruoyi.framework.manager.factory.AsyncFactory;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.combat.mapper.apprisk.CombatPhoneMapper;
import com.ruoyi.combat.domain.apprisk.CombatPhone;
import com.ruoyi.combat.service.apprisk.ICombatPhoneService;

/**
 * 【请填写功能名称】Service业务层处理
 *
 * @author ruoyi
 * @date 2024-08-21
 */
@Service
public class CombatPhoneServiceImpl implements ICombatPhoneService {
    @Autowired
    private ICombatPhoneAppService combatPhoneAppService;
    @Autowired
    private CombatPhoneMapper combatPhoneMapper;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    private CombatAppRiskMapper combatAppRiskMapper;

    /**
     * 查询【请填写功能名称】
     *
     * @param phoneId 【请填写功能名称】主键
     * @return 【请填写功能名称】
     */
    @Override
    public CombatPhone selectCombatPhoneByPhoneId(Long phoneId) {
        CombatPhone combatPhone = combatPhoneMapper.selectCombatPhoneByPhoneId(phoneId);
        List<Long> appidList = new ArrayList<>();
        if (combatPhone != null) {
            CombatPhoneApp combatPhoneApp = new CombatPhoneApp();
            combatPhoneApp.setPhoneId(phoneId);
            List<CombatPhoneApp> combatPhoneApps = combatPhoneAppService.selectCombatPhoneAppList(combatPhoneApp);
            combatPhone.setApkNum(combatPhoneApps.size());
            for (CombatPhoneApp combatPhoneApp1 : combatPhoneApps) {
                appidList.add(combatPhoneApp1.getAppId());
            }
        }
        List<Long> errorIds = new ArrayList<>();
        for (Long appid : appidList) {
            CombatAppRisk combatAppRisk = new CombatAppRisk();
            combatAppRisk.setAppId(appid);
            List<CombatAppRisk> combatAppRisks = combatAppRiskMapper.selectCombatAppRiskList(combatAppRisk);
            if (combatAppRisks.size() > 0) {
                errorIds.add(appid);
            }
        }
        combatPhone.setRiskApkNum(errorIds.size());
        return combatPhone;
    }

    @Override
    public CombatPhone selectCombatPhoneByPhoneNum(String phoneNum) {
        return combatPhoneMapper.selectCombatPhoneByPhoneNum(phoneNum);
    }

    /**
     * 查询【请填写功能名称】列表
     *
     * @param combatPhone 【请填写功能名称】
     * @return 【请填写功能名称】
     */
    @Override
    public List<CombatPhone> selectCombatPhoneList(CombatPhone combatPhone) {
        return combatPhoneMapper.selectCombatPhoneList(combatPhone);
    }

    /**
     * 新增【请填写功能名称】
     *
     * @param combatPhone 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int insertCombatPhone(CombatPhone combatPhone) {
        return combatPhoneMapper.insertCombatPhone(combatPhone);
    }

    /**
     * 修改【请填写功能名称】
     *
     * @param combatPhone 【请填写功能名称】
     * @return 结果
     */
    @Override
    public int updateCombatPhone(CombatPhone combatPhone) {
        return combatPhoneMapper.updateCombatPhone(combatPhone);
    }

    /**
     * 批量删除【请填写功能名称】
     *
     * @param phoneIds 需要删除的【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCombatPhoneByPhoneIds(Long[] phoneIds) {
        return combatPhoneMapper.deleteCombatPhoneByPhoneIds(phoneIds);
    }

    /**
     * 删除【请填写功能名称】信息
     *
     * @param phoneId 【请填写功能名称】主键
     * @return 结果
     */
    @Override
    public int deleteCombatPhoneByPhoneId(Long phoneId) {
        return combatPhoneMapper.deleteCombatPhoneByPhoneId(phoneId);
    }

    @Override
    public String register(AppUserRegisterBody registerBody) {
        String msg = "", username = registerBody.getUsername(), password = registerBody.getPassword();
        SysUser sysUser = new SysUser();
        sysUser.setUserName(username);

        // 验证码开关


        if (StringUtils.isEmpty(username)) {
            msg = "用户名不能为空";
        } else if (StringUtils.isEmpty(password)) {
            msg = "用户密码不能为空";
        } else if (username.length() < UserConstants.USERNAME_MIN_LENGTH
                || username.length() > UserConstants.USERNAME_MAX_LENGTH) {
            msg = "账户长度必须在2到20个字符之间";
        } else if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
                || password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
            msg = "密码长度必须在5到20个字符之间";
        } else if (!userService.checkUserNameUnique(sysUser)) {
            msg = "保存用户'" + username + "'失败，注册账号已存在";
        } else if (StringUtils.isEmpty(registerBody.getPhoneNum())) {
            msg = "注册失败,请联系系统管理人员";
        } else {

            sysUser.setNickName(username);
            sysUser.setPassword(SecurityUtils.encryptPassword(password));
            sysUser.setPhonenumber(registerBody.getPhoneNum());
            //设置用户角色：app用户
            sysUser.setRoleId(3l);
            sysUser.setRoleIds(new Long[]{3l});


            CombatPhone combatPhone = new CombatPhone();
            combatPhone.setPhoneNum(registerBody.getPhoneNum());
            List<CombatPhone> combatPhones = combatPhoneMapper.selectCombatPhoneList(combatPhone);
            if (combatPhones.size() > 0) {
                msg = "该手机号已注册";
                return msg;
            }
            boolean regFlag = userService.registerUser(sysUser);

            if (StringUtils.isEmpty(registerBody.getPhoneName())) {
                combatPhone.setPhoneName(username + "的" + registerBody.getBrand());
            } else {
                combatPhone.setPhoneName(registerBody.getPhoneName());
            }

            combatPhone.setPhoneRecordno(registerBody.getClientId());
            combatPhone.setPhoneState("离线");
            combatPhone.setPhoneFirstAccess(DateUtils.dateTimeNow(DateUtils.YYYY_MM_DD_HH_MM_SS));
            combatPhone.setPhoneBand(registerBody.getBrand());
            combatPhone.setPhoneOs(registerBody.getAndroidID());
            combatPhone.setUserId(sysUser.getUserId());
            int i = combatPhoneMapper.insertCombatPhone(combatPhone);
            if (i == 0) {
                msg = "注册失败,请联系系统管理人员";
            }

            if (!regFlag) {
                msg = "注册失败,请联系系统管理人员";
            } else {
                AsyncManager.me().execute(AsyncFactory.recordLogininfor(username, Constants.REGISTER, MessageUtils.message("user.register.success")));
            }
        }
        return msg;
    }
}
